<template>
  <div class="container">
    <!-- 导航固定定位 fixed -->
    <van-nav-bar
      fixed
      title="搜索结果"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 文章列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      @load="onLoad"
      finished-text="没有更多了"
    >
      <van-cell v-for="item in list" :key="item.art_id">
        <div class="article_item">
          <h3 class="van-ellipsis">{{ item.title }}</h3>
          <div class="img_box">
            <van-image
              :class="item.cover.type === 1 ? 'w100' : 'w33'"
              fit="cover"
              v-for="(imgsrc, index) in item.cover.images"
              :key="index"
              :src="imgsrc"
            >
              <template v-slot:loading>
                <van-loading type="spinner" size="20" />
              </template>
            </van-image>
            <!-- <van-image
              class="w33"
              fit="cover"
              src="https://img.yzcdn.cn/vant/cat.jpeg"
            /> -->
          </div>
          <div class="info_box">
            <span>{{ item.aut_name }}</span>
            <span>{{ item.comm_count }}评论</span>
            <span>{{ item.pubdate | dateFrom }}</span>
          </div>
        </div>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import { getSearchResultsApi } from '@/api/search'
export default {
  name: 'search-result',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      // params: { page: this.page, ...this.$route.query },
      page: 1
    }
  },
  computed: {
    params () {
      return { page: this.page, ...this.$route.query }
    }
  },
  methods: {
    async onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // setTimeout(() => {
      //   for (let i = 0; i < 10; i++) {
      //     this.list.push(this.list.length + 1)
      //   }
      const { data } = await getSearchResultsApi(this.params)
      console.log(data)
      this.list.push(...data.results)
      console.log(this.list)
      // 加载状态结束
      this.loading = false

      // 数据全部加载完成
      if (data) {
        this.page = this.page + 1
      } else {
        this.finished = true
      }
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  padding-top: 46px;
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.article_item {
  h3 {
    font-weight: normal;
    line-height: 2;
  }
  .img_box {
    display: flex;
    justify-content: space-between;
    .w33 {
      width: 33%;
      height: 90px;
    }
    .w100 {
      height: 180px;
      width: 100%;
    }
  }
  .info_box {
    color: #999;
    line-height: 2;
    position: relative;
    span {
      padding-right: 10px;
    }
  }
}
</style>
